package unipd.esp1112.pro;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;


/**
 * activity where the user can view the list of his recorded training sessions and then access 
 * more detailed data of each single recording
 * 
 * @author andrea basso, federico lancerin, alex tomasello
 * 
 */

public class Storico extends Activity {

	/**
	 * variable to store the name of the profile the user is currently using
	 */
	private String nomeProfilo;
	/**
	 * layout that contains the list of activities retrieved from the database  
	 */
	private LinearLayout layout;
	/**
	 * variable used to retrieve an instance of the application private database
	 */
	private MyOpenHelper op;
	/**
	 * variable used to reference the database
	 */
	private SQLiteDatabase db;
	/**
	 * int where is stored the width of the display where the application is currently running
	 */
	private int width;
	/**
	 * cursor used to retrieve the results of a query
	 */
	private Cursor query;
	/**
	 * button created for every training record found in the database that match the current profile name.
	 * it leads to "StoricoDettagli" activity
	 */
	private Button oldItem;
	/**
	 * auxiliary variable called from another activity to kill this one and go directly back to the main menu
	 */
	public static boolean ritorno = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent intent = getIntent();
		nomeProfilo = intent.getStringExtra("profilo");
		setContentView(R.layout.history);
		Display display = getWindowManager().getDefaultDisplay();
		width = display.getWidth();
	}

	protected void onResume() {
		super.onResume();

		if (ritorno) {
			ritorno = false;
			System.out.println("Menu terminato");
			finish();
		}

		layout = (LinearLayout) findViewById(R.id.activitySelectionLayout);
		layout.removeAllViewsInLayout();
		/*
		 * prendo un riferimento ad database e faccio una query per avere una lista delle attività di uno stesso profilo.
		 */

		op = new MyOpenHelper(this);
		db = op.getReadableDatabase();
		query = db.rawQuery("SELECT * FROM attivita where nome='" + nomeProfilo + "'", null);
		// creo un bottone per ogni attività esistente e lo aggiungo al layout
		while (!query.isLast() && !query.isAfterLast()) {
			query.moveToNext();
			oldItem = new Button(this);
			oldItem.setTypeface(null, Typeface.BOLD_ITALIC);
			oldItem.setBackgroundResource(R.drawable.customcoloredbutton);
			oldItem.setTextColor(Color.rgb(255, 255, 128));
			oldItem.setOnClickListener(new ItemChoose(oldItem));
			oldItem.setWidth(width);
			oldItem.setText(query.getString(2) + " - " + query.getString(1));
			layout.addView(oldItem);
		}
		if (query.getCount() == 0) {
			Toast toast = Toast.makeText(getApplicationContext(), "Nessuna attività registrata", Toast.LENGTH_SHORT);
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();
		}

		db.close();
	}

	private class ItemChoose implements OnClickListener {
		private Button itemChoose;

		private ItemChoose(Button iC) {
			itemChoose = iC;
		}

		@Override
		public void onClick(View v) {
			Intent myIntent = new Intent(getApplicationContext(), StoricoDettagli.class);
			myIntent.putExtra("tipodata", itemChoose.getText());
			startActivityForResult(myIntent, 0);
		}

	}

}
